package net.bsayiner.foreignExchange.Activities;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

import net.bsayiner.foreignExchange.R;
import net.bsayiner.foreignExchange.Utilities.Currency;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdRequest.Gender;
import com.google.ads.AdView;

public class CalculatorActivity extends Activity {

	private ArrayList<Currency> currencies;
	private ArrayList<Currency> marketCurrencies;
	private TextView txtInput;
	private TextView txtOutput;
	private Spinner spnInputValue;
	private Spinner spnOutputValue;
	private RadioGroup radioGroup;
	private ArrayAdapter<CharSequence> spnAdapter;
	private NumberFormat currencyFormat;
	private boolean isTcmb = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calculate_layout);
		currencies = getIntent().getParcelableArrayListExtra("Currency");
		marketCurrencies = getIntent().getParcelableArrayListExtra("CurrencyMarket");
		currencies.get(0).setCurrencyName("TRY");
		currencies.get(0).setBuying(1);
		currencies.get(0).setSelling(1);
		marketCurrencies.get(0).setCurrencyName("TRY");
		marketCurrencies.get(0).setBuying(1);
		marketCurrencies.get(0).setSelling(1);
		initializeComponents();
		spnAdapter = ArrayAdapter.createFromResource(this, R.array.unit_types, android.R.layout.simple_spinner_item);
		spnAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnInputValue.setAdapter(spnAdapter);
		spnOutputValue.setAdapter(spnAdapter);
	}

	private void initializeComponents() {
		currencyFormat = NumberFormat.getNumberInstance(Locale.US);
		currencyFormat.setMinimumFractionDigits(2);
		currencyFormat.setMaximumFractionDigits(2);
		txtInput = (TextView) findViewById(R.id.txtInput);
		txtOutput = (TextView) findViewById(R.id.txtOutput);
		spnInputValue = (Spinner) findViewById(R.id.spnInputValue);
		spnOutputValue = (Spinner) findViewById(R.id.spnOutputValue);
		radioGroup=findViewById(R.id)
		initializeEvents();
	}

	private void initializeEvents() {
		txtInput.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				calculateResult();
			}

			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			public void afterTextChanged(Editable s) {
			}
		});
		spnInputValue.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				calculateResult();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		spnOutputValue.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				calculateResult();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}

	private void calculateResult() {
		if (txtInput.getText().length() > 0) {
			try {
				double inputValue = Double.valueOf(txtInput.getText().toString());
				double tlValue = inputValue * currencies.get(spnInputValue.getSelectedItemPosition()).getSelling();
				double currencyResult = tlValue / (currencies.get(spnOutputValue.getSelectedItemPosition()).getSelling());
				txtOutput.setText(currencyFormat.format(currencyResult));
			} catch (Exception e) {
				txtOutput.setText("Illegal Input Value");
			}
		}
	}

	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.menu_calculator, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_calculator_about:
			PackageInfo packageInfo = null;
			try {
				packageInfo = getPackageManager().getPackageInfo("net.bsayiner.foreignExchange", 0);
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
			final AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle("Türk Lirası v" + packageInfo.versionName);
			alert.setMessage("Bu program Bora SAYINER tarafından yazılmış ve  OSI onaylı açık kaynak lisansı altında dağıtılmaktadır. Soru ve görüşleriniz için bsayiner@bsayiner.net adresine e-posta gönderebilirsiniz.");
			alert.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			alert.show();
			return true;
		case R.id.menu_calculator_exit:
			System.exit(0);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
